﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class createuser_free : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!Page.IsPostBack)
        {
            txtjigou.Text = "远程教育平台";
            RadioButtonList2.SelectedIndex = -1;
            
        }

    }
    protected void txtUsername_TextChanged(object sender, EventArgs e)
    {
        string zusername = txtUsername.Text.Trim();

        if (Membership.GetUser(zusername) != null)
        {

            //string noth = "“" + zusername + "”已存在，请重新输入用户名";
            Response.Write("<script language=javascript>alert('用户名已存在，请重新选择用户名！');if(opener!=null){opener.location.reload();window.close();}</script>");
            txtUsername.Text = "";
            txtUsername.Focus();
        }
        else
        {
            txtPwd.Focus();
        }
    }

    protected void txtEmail_TextChanged(object sender, EventArgs e)
    {
        string zuseremail = txtEmail.Text.Trim();

        if (!string.IsNullOrEmpty(Membership.GetUserNameByEmail(zuseremail)))
        {
            Response.Write("<script language=javascript>alert('该邮箱地址已存在，请重新输入邮箱！');if(opener!=null){opener.location.reload();window.close();}</script>");
            txtEmail.Text = "";
            txtEmail.Focus();
        }
        else
        {
            txtWenti.Focus();
        }
    }

    protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {
        string zusername = txtUsername.Text.Trim();
        string zpwd = txtPwd.Text.Trim();
        string zmail = txtEmail.Text.Trim();
        string zq1 = txtWenti.Text.Trim();
        string zq2 = txtWenti1.Text.Trim();
        string zusername1 = TextBox1.Text.ToString().Trim();
        string zgzdw = TextBox2.Text.ToString().Trim();
        string zcity = TextBox3.Text.ToString().Trim();
        string zmobile = TextBox4.Text.ToString().Trim();
        string zemail1 = TextBox5.Text.ToString().Trim();
        string zgrjj = TextBox6.Text.ToString().Trim();
        string zjigou = txtjigou.Text.ToString().Trim();
        string zxtsj1 = DateTime.Now.ToString();
        string zsql;
        string zrole= RadioButtonList1.SelectedItem.Value.TrimEnd();
        string zcreateman;

        if (Session["sUserName"] == null)   //说明是来自用户自行注册
        {
            zcreateman = "自由注册";
        }
        else
        {
            zcreateman = Session["sRole"].ToString() + Session["sUserName"].ToString();
        }

        zsql = "insert into [account] (role,username,mail,username1,gzdw,city,mobile,email,grjj,jigou,createman,xtsj1) values ('学员','" + zusername + "','" + zmail + "','" + zusername1 + "','" + zgzdw + "','" + zcity + "','" + zmobile + "','" + zemail1 + "','" + zgrjj + "','" + zjigou + "','" + zcreateman + "','" + zxtsj1 + "')";
        //以上搜集必要数据
        
        //以下开始新建用户
        MembershipCreateStatus result;
        Membership.CreateUser(zusername, zpwd, zmail, zq1, zq2, true, out result);
        string zresult = "";
        switch (result)
        {
            case MembershipCreateStatus.Success:    //添加系统数据成功，可以添加角色和用户表account

                //以下添加account表

                //以下判断是否添加成功，若不成功，彻底删除该用户，并提示。
                Conn conn = new Conn();
                if (conn.ExeSql(zsql) == 1) //若成功，则添加角色，否则提示出错，并删除系统表中该用户。
                {
                    Roles.AddUserToRole(zusername, zrole);  //添加角色,注意该方法不返回任何值，只要输入2个string变量即可通过！即直接向系统表添加。
                    conn.Dispose();
                    Response.Write("<script language=javascript>alert('操作正确，创建用户成功！');if(opener!=null){opener.location.reload();window.close();}</script>");
                    //Response.Redirect("../index.aspx");//在框架中此命令不能返回到父框架。尝试用其他方式如下：
                    //Response.Write(" <script> top.location.href='../index.aspx'; </script> ");
                    //Response.Redirect("createuserwizard.aspx"); //如果用这个命令，则提示信息用户看不到。
                    Response.Write(" <script> location.href='../index.aspx'; </script> ");

                }//直接执行，暂时不考虑失败情况
                else   //若向用户表中添加用户失败，则删除系统表中的对应数据，相当于没有作任何操作。
                {
                    conn.Dispose();
                    Membership.DeleteUser(zusername, true);
                    zresult = "建立用户表失败。";
                }
                break;

            case MembershipCreateStatus.InvalidUserName:
                zresult = "输入了非法的用户名。";
                break;
            case MembershipCreateStatus.InvalidPassword:
                zresult = "输入的密码不符合规范，必须7位数以上，且必须包含至少1位非字母非数字字符，如“~！·#￥%……—*”。";
                break;

            case MembershipCreateStatus.InvalidEmail:
                zresult = "输入了不符合要求的电子邮件信箱，请重新输入。";
                break;
            case MembershipCreateStatus.InvalidQuestion:
                zresult = "输入的安全问题不符合要求，请重新输入。";
                break;
            case MembershipCreateStatus.InvalidAnswer:
                zresult = "输入的安全问题回答不符合要求，请重新输入。";
                break;
            case MembershipCreateStatus.DuplicateUserName:
                zresult = "用户名重复，请重新选择用户名。";
                break;
            case MembershipCreateStatus.DuplicateEmail:
                zresult = "用户信箱重复，请重新输入。";
                break;
            default:
                zresult = "非常抱歉地通知您，创建用户时出现了不可预知的错误，请重新创建或联络系统管理员。";
                break;
        }
        zresult = "<script language=javascript>alert('" + zresult + "');if(opener!=null){opener.location.reload();window.close();}</script>";
        Response.Write(zresult);


    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtjigou.Text = DropDownList1.SelectedValue;
        txtjigou.Focus();
    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        
        
    }
}
